package com.demo.springbooot_demo.service;


import com.demo.springbooot_demo.pojo.User;
import com.demo.springbooot_demo.pojo.dto.UserDto;
import com.demo.springbooot_demo.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Autowired
    UserRepository userRepository;




    @Override
    public User add(UserDto user){
        User userPojo=new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);

    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).<IllegalArgumentException>orElseThrow(() -> {
            throw new IllegalArgumentException("用户不存在，参数异常!");
        });
    }

    @Override
    public User edit(UserDto user){
        User userPojo=new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);

    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);
    }
}
